package com.ccc.demoboot.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ccc.demoboot.common.NoModelWriteExcel;
import com.ccc.demoboot.common.R;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.prefs.NodeChangeEvent;

/**
 * 通用的controller
 *
 * @Author wz
 * @Date 2024/7/25 9:21
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: CommonController
 */
public class CommonController<S extends IService> {

    @Autowired
    S iService;

    /**
     *
     * {
     *     "where": [
     *         {
     *             "field": "label",
     *             "fieldvalue": "管理",
     *             "m": "like"
     *         },
     *         {
     *             "field": "id",
     *             "fieldvalue": "10",
     *             "m": "eq"
     *         }
     *     ]
     * }
     *
     * @return
     */
    @PostMapping("/selectcommon/{page}/{size}")
    public R selectCommon(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody Map<String, Object> map) {
        if (page > 0) {
            PageHelper.startPage(1, size);
        }

        QueryWrapper<Object> objectQueryWrapper = new QueryWrapper<>();
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) map.get("where");
        if (mapList != null) {
            for (Map<String, Object> objectMap : mapList) {
                String field = (String) objectMap.get("field");
                String fieldvalue = (String) objectMap.get("fieldvalue");
                String m = (String) objectMap.get("m");
                if (m.equals("like")) {
                    objectQueryWrapper.like(field, fieldvalue);
                } else {
                    objectQueryWrapper.eq(field, fieldvalue);
                }
            }
        }

        List list = this.iService.list(objectQueryWrapper);
        return new R().add("list", list);
    }


    /**
     * @throws IOException
     */
    @GetMapping("/uploadexcel")
    public void uploadExcel(HttpServletResponse response,String filename) throws IOException {
        List<Map<String,Object>> listMaps = this.iService.listMaps();
        NoModelWriteExcel noModelWriteExcel = new NoModelWriteExcel();
        List<List<String>> head = noModelWriteExcel.head(listMaps);//excel头
        List<List<Object>> lists = noModelWriteExcel.dataList(listMaps);

        response.setContentType("application/x-tar;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename="
                + java.net.URLEncoder.encode(filename+".xlsx", "UTF-8"));


        EasyExcel.write(response.getOutputStream()).head(head).sheet("导出数据 ").doWrite(lists);


    }

}
